import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.FileHandler;
import java.util.logging.Logger;

import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.UUID;
import javax.microedition.io.StreamConnectionNotifier;

import bluetooth.BtUtilException;

public class PcktPC {
	public static String MODO = "Direta";  
	
	//Dispositivo local
	private static LocalDevice localDevice;
	//UUID do servico
	private final static UUID UUID = new javax.bluetooth.UUID("0000110100001000800000805F9B34FB",false);
	//private final static String UUID = "d90ec0cb-b8c0-4e3c-9168-5893d25c19fc";
	//URL do Bluetooth
	private static String url = "btspp://localhost:" + UUID.toString() + ";name=rfcommtest;authorize=false;authenticate=false";
	
	private static Logger logger;
	private static FileHandler fh;
	
	private static StreamConnectionNotifier strmNotf;
	
	private static Set<ConnectedThreadJava> threads;

	/**
	 * @param args
	 * @throws BtUtilException 
	 * @throws IOException 
	 */
	//TODO: Achar uma forma de deixar parear sem precisar colocar LINK_KEY
	public static void main(String[] args) {
		threads = new HashSet<ConnectedThreadJava>();
		logger = Logger.getLogger("pckt.Test");
		
		try {
			fh = new FileHandler("Log.txt");
		} catch (Exception e) {
			logger.warning("Couldn't open file handler for logger: Main");
		}
		logger.addHandler(fh);
		
		if(!BTInicialization()){
			System.exit(0);
		}
		
		//Open the thread to listen to income conections
		ConnectingThreadJava connecting = new ConnectingThreadJava(strmNotf, threads, url);
		connecting.start();
		
//		for(int i =0; i< 50000;i++){}
//		connecting.close();
//		System.out.println(connecting.getName());
		
	}
	//TODO: Checar se ele consegue enviar eqnto recebe
	public static void sendFile(ConnectedThreadJava thread, String name){
		StorageHandler readHandler = new StorageHandler(name);
		readHandler.openRead();
		
		thread.write(readHandler.read());
		
	}
	
	public static Boolean BTInicialization(){
		try {
			localDevice = LocalDevice.getLocalDevice();
		} catch (BluetoothStateException e) {
			logger.warning("Couldn't set up LocalDevice");
			return false;
		}
		//Set the device to always be in discovery mode
		try {
			localDevice.setDiscoverable(DiscoveryAgent.GIAC);
		} catch (BluetoothStateException e) {
			logger.warning("Couldn't set device in discoverable mode");
			return false;
		}
		return true;		
	}
	
	
}
